package com.duolaibei.business.view.goods;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.utils.log.LogDebug;
import com.duolaibei.business.view.goods.fragment.BarCodeFragment;
import com.duolaibei.business.view.goods.fragment.RetailPriceFragment;
import com.duolaibei.business.view.goods.mode.BatchBarcodeBean;
import com.duolaibei.business.view.goods.mode.FormatsBean;
import com.duolaibei.business.view.goods.mode.FormatsBeanManager;
import com.duolaibei.business.view.goods.mode.RetailPriceBean;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class BatchSetupActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mBack;
    @Inject(R.id.title)
    private TextView mTxtTitle;
    @Inject(R.id.btn_batchs_edit)
    private TextView mBtnSave;
    @Inject(R.id.batch_tablayout)
    private TabLayout mTabLayout;
    @Inject(R.id.viewpager)
    private ViewPager mViewPager;

    private BarCodeFragment barCodeFragment;
    private RetailPriceFragment retailPriceFragment;
    private List<BatchBarcodeBean> barcodeBeanList;//条形码
    private List<RetailPriceBean> retailPriceBeanList;//

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_batch_setup);
    }

    @Override
    protected void initViews() {
        mTxtTitle.setText("批量编辑");
        //TabLayout设置分割线
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.divider));
        linearLayout.setDividerPadding(dp2px(12)); //设置分割线间隔

    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
//        ViewPager
//        https://blog.csdn.net/danhantao/article/details/76522063
        initFragment();
        getFormatsList();
    }

    /**
     * 获取保存的商品规格
     */
    private void getFormatsList() {
        //读取数据库保存的规格
        List<FormatsBean> list = FormatsBeanManager.getInstance().selectAllFormats();
        List<FormatsBean> tempList = new ArrayList<>();
        for (FormatsBean formatsBean : list) {
            if (formatsBean.getIsChoosed()) {
                tempList.add(formatsBean);
            }
        }
        barcodeBeanList = new ArrayList<>();
        retailPriceBeanList = new ArrayList<>();
        List<List<FormatsBean.DataBean>> lists = new ArrayList<>();
        for (FormatsBean formatsBean : tempList) {
            lists.add(formatsBean.getData());
        }
        List<String> formats = null;
        List<List<String>> formatsList = new ArrayList<>();
        List<String> resultList = new ArrayList<>();
        for (List<FormatsBean.DataBean> dataBeanList : lists) {
            formats = new ArrayList<>();
            for (int i = 0; i < dataBeanList.size(); i++) {
                String state = dataBeanList.get(i).getState();
                if ("1".equals(state)) {
                    formats.add(dataBeanList.get(i).getName());
                }
            }
            formatsList.add(formats);
        }
        // formatsList 包含最多三个数组规格名字的集合
        int a = formatsList.size();
        StringBuilder stringBuilder = null;
        if (a == 1) {
            for (int i = 0; i < formatsList.get(0).size(); i++) {
                stringBuilder = new StringBuilder();
                stringBuilder.append(formatsList.get(0).get(i));
                resultList.add(stringBuilder.toString());
            }
        } else if (a == 2) {
            for (int i = 0; i < formatsList.get(0).size(); i++) {
                for (int j = 0; j < formatsList.get(1).size(); j++) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append(formatsList.get(0).get(i));
                    stringBuilder.append(";").append(formatsList.get(1).get(j));
                    resultList.add(stringBuilder.toString());
                }
            }
        } else if (a == 3) {
            for (int i = 0; i < formatsList.get(0).size(); i++) {
                for (int j = 0; j < formatsList.get(1).size(); j++) {
                    for (int k = 0; k < formatsList.get(2).size(); k++) {
                        stringBuilder = new StringBuilder();
                        stringBuilder.append(formatsList.get(0).get(i));
                        stringBuilder.append(";").append(formatsList.get(1).get(j));
                        stringBuilder.append(";").append(formatsList.get(2).get(k));
                        resultList.add(stringBuilder.toString());
                    }
                }
            }
        }
        // 组合完成，开始组装批量设置中的名称，单个商品的规格
        BatchBarcodeBean batchBarcodeBean;
        RetailPriceBean retailPriceBean;
        for (String str : resultList) {
            LogDebug.d("---------resultList----------" + str);
        }
    }

    private void initFragment() {
        List<Fragment> list = new ArrayList<>();
        barCodeFragment = new BarCodeFragment();
        retailPriceFragment = new RetailPriceFragment();
        list.add(barCodeFragment);
        list.add(retailPriceFragment);
        BatchSetupAdapter batchSetupAdapter = new BatchSetupAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(batchSetupAdapter);
    }

    private class BatchSetupAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public BatchSetupAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }


    @Override
    protected void onSetListener() {
        mBack.setOnClickListener(getThis());
        mBtnSave.setOnClickListener(getThis());
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        } else if (v.getId() == R.id.btn_batchs_edit) {

        }
    }


    private int dp2px(float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getThis().getResources().getDisplayMetrics());
    }


}
